package HashTable;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/*给你两个字符串：ransomNote 和 magazine ，判断 ransomNote 能不能由 magazine 里面的字符构成。
如果可以，返回 true ；否则返回 false 。
magazine 中的每个字符只能在 ransomNote 中使用一次

示例 1：
输入：ransomNote = "a", magazine = "b"
输出：false

示例 2：
输入：ransomNote = "aa", magazine = "ab"
输出：false

示例 3：
输入：ransomNote = "aa", magazine = "aab"
输出：true
* */
public class lc383 {
    public static void main(String[] args) {
        String ransomNote = "aa", magazine = "aab";
        System.out.println(canConstruct(ransomNote, magazine));

    }
    public static boolean canConstruct(String ransomNote, String magazine) {
          char[] chars1=ransomNote.toCharArray();
          char[] chars2=magazine.toCharArray();
          //分别储存两个字符串中各个字母的出现次数
        //若ransomNote中的字母都在magazine中，且magazine中的字母出现次数≥ransomNote每个字母的出现次数
        //返回true
        HashMap<Character,Integer> map1=new HashMap<>();
        for (int i = 0; i <chars1.length ; i++) {
            if(!map1.containsKey(chars1[i])){
                map1.put(chars1[i],1);
            }else{
                int count=map1.get(chars1[i]);
                count++;
                map1.put(chars1[i],count);
            }
        }

        HashMap<Character,Integer> map2=new HashMap<>();
        for (int i = 0; i <chars2.length ; i++) {
            if(!map2.containsKey(chars2[i])){
                map2.put(chars2[i],1);
            }else{
                int count=map2.get(chars2[i]);
                count++;
                map2.put(chars2[i],count);
            }
        }
        Set set1= map1.keySet();
        Set set2= map2.keySet();
        if (!set2.containsAll(set1)){
            return false;
        }

        for (Map.Entry e:map1.entrySet()
             ) {
            int count1= (int) e.getValue();
            char c= (char) e.getKey();
            int count2=map2.get(c);
            if(count2<count1){
                return false;
            }
        }

        return true;
    }
    public static boolean canConstruct2(String ransomNote, String magazine) {
        //本题只涉及到26个小写字母，因此可以用用数组充当hashtable
        //统计第一个字符串中各个字母的出现次数
       return true;
    }
}
